package com.wsc.wisdomchina.activitity.wisdomParty;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.JsonTools;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchina.callback.ContributorView;
import com.wsc.wisdomchina.constant.Constant;
import com.wsc.wisdomchina.javaBean.RespObj;
import com.wsc.wisdomchina.javaBean.wisdomParty.PartyUserBean;
import com.wsc.wisdomchina.presenter.ContributorPresenter;


public class PartyUserActivity extends BaseActivity implements View.OnClickListener, ContributorView {
    private ImageView iv_back;
    private Context context;
    private TextView tv_guize;
    private TextView tv_name;
    private TextView tv_age;
    private Button bt_finish;
    private TextView tv_affiliated;
    private TextView tv_partyDuties;
    private TextView tv_admDuties;
    private TextView tv_joinPartyDate;
    private TextView tv_contactNumber;
    private ImageView iv_headImg;
    private String titleIntent;
    private String idIntent;
    private TextView tv_headtitle;
    private ContributorPresenter contributorPresenter;
    private Context mcontext;
    private final String PartyFragmentString = PartyUserActivity.class.getSimpleName();
    private final String UPDATESTRING="updateStringFlag";
    private RelativeLayout partyActivityApply;
    private TextView tv_jifeng;
    private TextView tv_paiming;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.partyuser_layout);
        Intent intent = getIntent();
        idIntent = intent.getStringExtra("id");
        titleIntent = intent.getStringExtra("title");
        mcontext = this;
        contributorPresenter = new ContributorPresenter(this, mcontext);
        initView();
        if (idIntent == null) {
            tv_headtitle.setText("个人党员资料");
            bt_finish.setVisibility(View.VISIBLE);
            partyActivityApply.setVisibility(View.VISIBLE);
            String message = Tools.getSharedPreferencesStrings(Constant.SharePreferencePartyUserInfo, "", context);
            if (message != null && message != "") {
                PartyUserBean partyUserBean = JsonTools.jsonStringToObject(message, PartyUserBean.class);
                contributorPresenter.getAddressListGetByIdMem(partyUserBean.getId(), UPDATESTRING);
            }
        } else {
            tv_headtitle.setText(titleIntent);
            bt_finish.setVisibility(View.GONE);
            partyActivityApply.setVisibility(View.GONE);
            contributorPresenter.getAddressListGetByIdMem(idIntent, PartyFragmentString);
        }
    }

    private void initView() {
        context = this;
        iv_back = (ImageView) findViewById(R.id.regist_back);
        tv_guize = (TextView) findViewById(R.id.tv_guize);
        iv_back.setOnClickListener(this);
        tv_guize.setOnClickListener(this);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_age = (TextView) findViewById(R.id.tv_age);
        bt_finish = (Button) findViewById(R.id.bt_finish);
        bt_finish.setOnClickListener(this);
        tv_affiliated = (TextView) findViewById(R.id.tv_affiliated);
        tv_partyDuties = (TextView) findViewById(R.id.tv_partyDuties);
        tv_admDuties = (TextView) findViewById(R.id.tv_admDuties);
        tv_joinPartyDate = (TextView) findViewById(R.id.tv_joinPartyDate);
        tv_contactNumber = (TextView) findViewById(R.id.tv_contactNumber);
        iv_headImg = (ImageView) findViewById(R.id.iv_headImg);
        tv_headtitle = (TextView) findViewById(R.id.tv_headtitle);
        //党建活动报名
        partyActivityApply = ((RelativeLayout) findViewById(R.id.rl_partyactivityapply));
        partyActivityApply.setOnClickListener(this);
        tv_jifeng= (TextView) findViewById(R.id.tv_jifeng);
        tv_paiming= (TextView) findViewById(R.id.tv_paiming);
    }

    private void showData(PartyUserBean partyUserBean) {
        tv_name.setText(partyUserBean.getName());
        tv_age.setText(partyUserBean.getAge() + "");
        tv_affiliated.setText(partyUserBean.getAffiliated());
        tv_partyDuties.setText(partyUserBean.getPartyDuties());
        tv_admDuties.setText(partyUserBean.getAdmDuties());
        tv_joinPartyDate.setText(Tools.formatDateTimeyyyyMMDD(Tools.formatData(partyUserBean.getJoinPartyDate())));
        tv_contactNumber.setText(partyUserBean.getContactNumber());
        Glide.with(context).load(partyUserBean.getHeadImg())
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.mipmap.me)
                .into((ImageView) iv_headImg);
        tv_jifeng.setText(partyUserBean.getIntegral()+"分");
        tv_paiming.setText(partyUserBean.getRanking()+"名");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.regist_back:
                finish();
                break;
            case R.id.tv_guize:
                break;
            case R.id.bt_finish:
                Tools.setSharedPreferencesStrings(Constant.SharePreferencePartyUserInfo, "", context);
                Intent intent = getIntent();
                setResult(1, intent);
                finish();
                break;
            case R.id.rl_partyactivityapply:
                Intent partyapply = new Intent(this, PartyApplyActivity.class);
                startActivity(partyapply);
                break;
        }
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }


    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            if (flag.equals(PartyFragmentString)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    PartyUserBean partyUserBean = JsonTools.jsonStringToObject(jsonMessage, PartyUserBean.class);
                    showData(partyUserBean);
                }
            }else if(flag.equals(UPDATESTRING))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    Tools.setSharedPreferencesStrings(Constant.SharePreferencePartyUserInfo,jsonMessage,context);
                    PartyUserBean partyUserBean = JsonTools.jsonStringToObject(jsonMessage, PartyUserBean.class);
                    showData(partyUserBean);
                }
            }
        } catch (Exception e) {

        }
    }


    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error, String flag) {
    }


}
